// store/index.js
import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user.js' // 引入用户模块

Vue.use(Vuex)

const store = new Vuex.Store({
  modules: {
    user
  },
  state: {
    // 全局状态
    token: localStorage.getItem('token') || '' // 初始化时从 localStorage 中获取
  },
  mutations: {
    // 全局 mutations
    SET_TOKEN(state, token) {
      state.token = token
      localStorage.setItem('token', token) // 将 token 存入 localStorage
    },
    CLEAR_TOKEN(state) {
      state.token = ''
      localStorage.removeItem('token') // 从 localStorage 中清除 token
    }
  },
  actions: {
    // 全局 actions
    setToken({ commit }, token) {
      commit('SET_TOKEN', token)
    },
    clearToken({ commit }) {
      commit('CLEAR_TOKEN')
    }
  },
  getters: {
    // 全局 getters
    getToken: state => state.token
  }
})

export default store
